import DKVui from "dk-vui";
import "dk-vui/dist/style.css";
import "xe-utils";
import VXETable from "vxe-table";
import XEUtils from "xe-utils";
import "vxe-table/lib/style.css";
import { formatNumber } from "@/utils/tools.js";
export default function (app) {
  app.use(DKVui);
  DKVui.setup({
    table: {
      scrollHideForm: true,
    },
  });
  VXETable.formats.mixin({
    //格式kg
    formatWeight({ cellValue }) {
      return cellValue && XEUtils.divide(cellValue, 1000);
    },
    //截取小数点后num位
    formatNumberData({ cellValue, num = 4 }) {
      return cellValue && formatNumber(cellValue, num);
    },
  });
  
  VXETable.renderer.add("opts", {
    // 状态展示
    renderDefault(renderOpts, params) {
      let { row, column } = params;
      let code = row[column.field];
      return h('span', {}, code?.label || "-");
    },
  });
  VXETable.renderer.add("ObjectMap", {
    //对象遍历
    renderDefault(renderOpts, params) {
      const { row, column } = params;
      const data = row[column.field];
      let renderContent = h("div"); // 默认的 VNode
      if (Array.isArray(data)) {
        // 如果是数组，渲染每个元素
        renderContent = data.map((item) =>
          h("div", [h("span", { class: "text-[#333]" }, item)])
        );
      } else if (typeof data === "object") {
        // 如果是对象，渲染键值对
        renderContent = Object.keys(data).map((key) =>
          h("div", [
            h("span", { class: "text-[#666]" }, `${key}: `),
            h(
              "span",
              { class: "text-[#333]" },
              data[key] ? JSON.stringify(data[key]) : ""
            ),
          ])
        );
      } else {
        // 其他情况直接渲染文本
        renderContent = h("div", [h("span", { class: "text-[#333]" }, data)]);
      }
      return renderContent;
    },
  });
}
